package com.suo.service.impl;

import com.suo.mapper.AdminMapper;
import com.suo.pojo.Admin;
import com.suo.pojo.AdminExample;
import com.suo.service.AdminService;
import com.suo.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    private AdminMapper mapper;

    @Autowired
    public void setMapper(AdminMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public Admin login(String name, String pwd) {
        //条件查询时用于封装条件
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andANameEqualTo(name);

        List<Admin> admins = mapper.selectByExample(example);

        if(admins.size() > 0) {
            Admin admin = admins.get(0);
            String md5 = MD5Util.getMD5(pwd);
            if(md5.equals(admin.getaPass())) {
                return admin;
            }
        }

        return null;
    }
}
